﻿using DevExpress.Web.ASPxTreeView;
using DevExpress.XtraReports.UI;
using System;
using System.Collections.Generic;
using System.Data;
using EntryExit.Utils;
using System.Web.UI;

namespace EntryExit
{
    public partial class DashBoardReport : BasePage
    {
        public int SerialCounter { get; set; }
        public ReportManager rptManager = new ReportManager();
        public XtraReport CurrentReport
        {
            get
            {
                return (XtraReport)Session[SessionKeys.REPORT_DATA];
            }
            set
            {
                Session[SessionKeys.REPORT_DATA] = value;
            }
        }
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                
                if (Session["Searched"] == "1")
                {
                    reportViewer.Report = null;
                    reportViewer.Width = System.Web.UI.WebControls.Unit.Percentage(100);
                    reportViewer.Height = System.Web.UI.WebControls.Unit.Percentage(100);
                    CurrentReport = null;
                }

                if (CurrentReport != null)
                {
                    reportViewer.Report = CurrentReport;
                    CurrentReport.DataSourceRowChanged += CurrentReport_DataSourceRowChanged;
                }

                Session["Searched"] = "0";
            }
            else // !IsPostBack
            {
                reportViewer.Report = null;
                CurrentReport = null;
                if (this.Master is MainMaster)
                {
                    ((MainMaster)this.Master).SetAllowCheckingInTreeview();
                    ((MainMaster)this.Master).DisableGroupedChecking();
                    ViewState[SessionKeys.GROUPED_CHECK_PREFERANCE_STATE] = false;
                }
                DefaultCalendarValues();
                calendarStart.MaxDate = System.DateTime.Now;
                calendarEnd.MaxDate = System.DateTime.Now;
            }
            
        }

        void CurrentReport_DataSourceRowChanged(object sender, DataSourceRowEventArgs e)
        {
            
            //throw new NotImplementedException();
        }

        private void DefaultCalendarValues()
        {
            DateTime today = DateTime.Now;
            int date = DateTime.DaysInMonth(today.Month == 1 ? today.Year - 1 : today.Year, today.Month == 1 ? 12 : today.Month - 1) >= DateTime.Today.Day ? DateTime.Today.Day : DateTime.DaysInMonth(today.Month == 1 ? today.Year - 1 : today.Year, today.Month == 1 ? 12 : today.Month - 1);
            // calendarStart.Date = new DateTime(today.Month == 1 ? today.Year - 1 : today.Year, today.Month == 1 ? 12 : today.Month - 1, date);
            calendarStart.Date = new DateTime(today.AddDays(-9).Year, today.AddDays(-9).Month, today.AddDays(-9).Day);
            calendarEnd.Date = new DateTime(today.Year, today.Month, DateTime.Today.Day);
        }
        protected void reportViewer_Unload(object sender, EventArgs e)
        {
            reportViewer.Report = null;
        }

        protected void btnGenerateReport_Click(object sender, EventArgs e)
        {
            try
            {
            DateTime startDate = calendarStart.Date;
            DateTime endDate = calendarEnd.Date;
            if (endDate < startDate)
            {
                lblError.Visible = true;
                lblError.Text = "Error : Selected end date is less than start date";
                
                // By Rajkumar on 24.02.2014
                // To clear reportviewer data. The last report data will persists in viewer unless we clear.
                reportViewer.Report = null;

                return;
            }
            if ((endDate - startDate).TotalDays > 9)
            {
                lblError.Visible = true;
                lblError.Text = " Maximum 10 Days allowed..!";

                // By Rajkumar on 24.02.2014
                // To clear reportviewer data. The last report data will persists in viewer unless we clear.
                reportViewer.Report = null;

                return;
            }
            reportViewer.Report = null;
            UpdateSelectedEmployees();
            if (SelectedEmployees.Count == 0)
            {
                lblError.Visible = true;
                lblError.Text = "No Employees Selected...";

                // By Rajkumar on 24.02.2014
                // To clear reportviewer data. The last report data will persists in viewer unless we clear.
                reportViewer.Report = null;

                return;
            }
            
            lblError.Visible = false;
            bool needHardLoading = false;
            
            if (CurrentReport == null)
            {
                needHardLoading = true;
            }
            //-----------------------
                CurrentReport = rptManager.rptDashBoard(SelectedEmployees, calendarStart.Date, calendarEnd.Date);
                reportViewer.Report = CurrentReport;
            
            //-----------------------
            if (needHardLoading)
            {
                this.Page_Load(null, null);
            }
        }
            catch (Exception ex)
            {
                Logger.Error("Page : DashBoardReport.aspx.cs, Event : btnGenerateReport_Click", ex);
                ScriptManager.RegisterStartupScript(this, GetType(), "showalert", "alert('An internal error occured. Reload the tool and try again. If issue still persists, please report to your administrator..!');", true);
            }
        }

        private void UpdateSelectedEmployees()
        {
            SelectedEmployees.Clear();
            List<Employee> employees=new List<Employee> ();
            if (this.Master is MainMaster)
            {
                employees = ((MainMaster)this.Master).GetAllSelectedEmployees();
            }
            SelectedEmployees = employees;

        }
        protected void btnGroupSelection_Click(object sender, EventArgs e)
        {
            if (this.Master is MainMaster)
            {
                if ((bool)ViewState[SessionKeys.GROUPED_CHECK_PREFERANCE_STATE])
                {
                    ViewState[SessionKeys.GROUPED_CHECK_PREFERANCE_STATE] = false;
                    ((MainMaster)this.Master).DisableGroupedChecking();
                    btnGroupedSelection.Text = "Enable Grouped Selection";
                }
                else
                {
                    ViewState[SessionKeys.GROUPED_CHECK_PREFERANCE_STATE] = true;
                    ((MainMaster)this.Master).EnableGroupedChecking();
                    btnGroupedSelection.Text = "Disable Grouped Selection";
                } 
            }
        }
       
    }
}